/******************************************************************************** /
 * DictionaryModelWriter.java - JPLearning Project								*
 * 																				*
 * 		This program is free software; you can redistribute it and/or modify	*
 *		it under the terms of the GNU General Public License as published by	*
 *		the Free Software Foundation; version 2 of the License.					*
 *																				*
 *		This program is distributed in the hope that it will be useful,			*
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of			*
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
 *		GNU General Public License for more details.							*
 *																				*
 ********************************************************************************/
package org.jplearning.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;
import org.jplearning.model.DictionaryModel;
import org.jplearning.model.WordEntry;
import org.jplearning.model.text.HiraganaString;
import org.jplearning.model.text.IText;
import org.jplearning.model.text.KanjiString;
import org.jplearning.model.text.KatakanaString;

/**
 * @author A144601
 */
public class DictionaryModelWriter {
	private static final Logger log = Logger.getLogger(DictionaryModelWriter.class);
	
	private DictionaryModel dictionaryModel;
	
	public DictionaryModelWriter(DictionaryModel model){
		this.dictionaryModel = model;
	}
	
	public void writeToFile(File file) throws UnsupportedEncodingException, IOException, Exception{
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		HashMap map = dictionaryModel.getEntries();
		Set keySet = map.keySet();
		Iterator itKeySet = keySet.iterator();
		
		while(itKeySet.hasNext()){
			IText iText = (IText)itKeySet.next();
			WordEntry entry = (WordEntry)map.get(iText);
			
			log.debug("(" + iText + " ," + entry + ")");
			
			bos.write(writeEntry(iText, entry).getBytes("UTF-8"));
			bos.write("\n".getBytes("UTF-8"));
		}
		
		bos.close();
		fos.close();
	}
	
	private String getType(IText cadena){
		if(cadena instanceof HiraganaString){
			return "HG";
		}
		else if(cadena instanceof KatakanaString){
			return "KT";
		}
		else if(cadena instanceof KanjiString){
			return "KJ";
		}
		else{
			return "?";
		}
	}
	
	private String writeEntry(IText cadena, WordEntry entry) throws Exception{
		StringBuffer buffer = new StringBuffer();
		
		String tipo = getType(cadena);
		buffer.append("[");
		buffer.append(tipo);
		buffer.append("]");
		
		buffer.append(cadena.getText());
				
		Iterator itSignificados = entry.getSignificados().iterator();
		while(itSignificados.hasNext()){
			String curSignificado = (String)itSignificados.next();
			String curTipo = WordEntry.getAbreviatura(entry.getTipoPalabra());
			buffer.append("/");
			buffer.append(curTipo);
			buffer.append(curSignificado);
		}
		buffer.append("/");
		return buffer.toString();
	}
}
